<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <div>
    <button id="connect">连接</button>
    <button id="disconnect" disabled="disabled">断开</button>
  </div>
  <div>
    <input type="text" id="message" name="message">
    <input type="button" id="sendmessage" value="send">
    <p id="response"></p>
  </div>
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/js/sockjs.min.js}"></script>
<script type="text/javascript" th:src="@{/js/stomp.min.js}"></script>
<script th:inline="javascript">
  $(function(){
	  var stompClient = null;
	  $("#connect").on("click",function(e){
		  var socket = new SockJS("/endpointWisely");
		  stompClient = Stomp.over(socket);
		  stompClient.connect({},function(fram){
			  setConnected(true);
			  console.log("开始连接!");
			  stompClient.subscribe("/topic/getResponse",function(response){
				  showResponse(JSON.parse(response.body).message);
			  });
		  });
	  });
	  $("#disconnect").on("click",function(e){
		  if (stompClient != null) {
              stompClient.disconnect();
          }
          setConnected(false);
          console.log("Disconnected");
	  });
	  
	  $("#sendmessage").on("click",function(e){
		        var message = $('#message').val();
		        stompClient.send("/welcome", {}, JSON.stringify({ 'message': message }));//5
	  });
	  
	  function setConnected(connected){
		  $("#connect").attr("disabled",connected);
		  $("#disconnect").attr("disabled",!connected);
	  }
	  
	  function showResponse(message) {
          var response = $("#response");
          response.html(message);
    }
  });
</script>
</body>
</html>